package com.fy.utils;


import com.fy.utils.result.GaodeApiResponse;
import com.fy.utils.result.IpApiResponse;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

public class IpUtils {

    private static final String GAODE_API_KEY = "98f437cd36a2e501929f4b4915652b3f"; // 替换为高德地图 API Key
    private static final String GAODE_API_URL = "https://restapi.amap.com/v3/ip?ip=%s&output=json&key=" + GAODE_API_KEY;

    /**
     * 获取当前请求的 IP 地址
     *
     * @param request HttpServletRequest 对象
     * @return 请求的 IP 地址
     */
    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 根据 IP 地址获取详细地址信息
     *
     * @return 详细地址信息
     */
    public static String getDetailedAddressByIp(String ip1) {
        String ip="118.31.247.175";
        RestTemplate restTemplate = new RestTemplate();
        String url = String.format(GAODE_API_URL, ip);
        GaodeApiResponse response = restTemplate.getForObject(url, GaodeApiResponse.class);

        if (response != null && "1".equals(response.getStatus())) {
            String province = response.getProvince();
            String city = response.getCity();
            String district = response.getDistrict();

            // 拼接地址信息，确保不包含 null 或其他乱七八糟的字符串
            StringBuilder addressBuilder = new StringBuilder();
            if (province != null && !province.isEmpty()) {
                addressBuilder.append(province);
            }
            if (city != null && !city.isEmpty()) {
                addressBuilder.append(city);
            }
            if (district != null && !district.isEmpty()) {
                addressBuilder.append(district);
            }

            String address = addressBuilder.toString();
            return address.isEmpty() ? null : address;
        }
        return null;
    }

    /**
     * 根据 IP 地址获取省份信息
     *
     * @param ip IP 地址
     * @return 省份信息
     */
    public static String getProvinceByIp(String ip) {
        RestTemplate restTemplate = new RestTemplate();
        String url = String.format("http://ip-api.com/json/%s?lang=zh-CN", ip);
        IpApiResponse response = restTemplate.getForObject(url, IpApiResponse.class);
        if (response != null && "success".equals(response.getStatus())) {
            return response.getRegionName();
        }
        return null;
    }
}
